﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BSE.Windows.Test
{
    public enum AccountType
    {
        DayAccount,
        DrugAccount
    }

    public partial class AccountForm : BaseForm
    {
        private AccountType accountTyep = AccountType.DayAccount;
        private DateTimePicker dayPicker = null;
        private DateTimePicker startPicker = null;
        private DateTimePicker endPicker = null;
        private bool initCompleted = false;

        public AccountForm()
        {
            InitializeComponent();
            InitToolStrip();
        }

        public AccountType AccountType
        {
            get { return accountTyep; }
            set 
            { 
                accountTyep = value; 
            }
        }

        private void InitToolStrip()
        {
            dayPicker = new DateTimePicker();
            dayPicker.Width = 110;
            dayPicker.Format = DateTimePickerFormat.Custom;
            dayPicker.ValueChanged += new EventHandler(toolStripComboBoxDrug_SelectedIndexChanged);
            ToolStripControlHost host1 = new ToolStripControlHost(dayPicker);
            toolStrip1.Items.Insert(0, host1);

            startPicker = new DateTimePicker();
            startPicker.Width = 110;
            startPicker.Format = DateTimePickerFormat.Custom;
            startPicker.Value = DateTime.Now.AddMonths(-1);
            startPicker.ValueChanged += new EventHandler(toolStripComboBoxDrug_SelectedIndexChanged);
            ToolStripControlHost host2 = new ToolStripControlHost(startPicker);
            toolStrip1.Items.Insert(3, host2);

            endPicker = new DateTimePicker();
            endPicker.Width = 110;
            endPicker.Format = DateTimePickerFormat.Custom;
            endPicker.ValueChanged += new EventHandler(toolStripComboBoxDrug_SelectedIndexChanged);
            ToolStripControlHost host3 = new ToolStripControlHost(endPicker);
            toolStrip1.Items.Insert(5, host3);
        }

        private void AccountForm_Load(object sender, EventArgs e)
        {
            RefreshForm();
            initCompleted = true;
        }

        public override void RefreshForm()
        {
            if (accountTyep == AccountType.DayAccount)
            {
                toolStripComboBoxDrug.Visible = false;
                toolStripLabelStart.Visible = false;
                toolStripLabelEnd.Visible = false;
                toolStrip1.Items[3].Visible = false;
                toolStrip1.Items[5].Visible = false;
                toolStrip1.Items[0].Visible = true;
            }
            else
            {
                toolStripComboBoxDrug.Visible = true;
                toolStripLabelStart.Visible = true;
                toolStripLabelEnd.Visible = true;
                toolStrip1.Items[3].Visible = true;
                toolStrip1.Items[5].Visible = true;
                toolStrip1.Items[0].Visible = false;
            }

            FillDrugList();
            LoadAccountList();
        }

        private void FillDrugList()
        {
            List<Yaopin> yaopins = AnesthesiaBLL.GetAllYaopin();
            toolStripComboBoxDrug.ComboBox.DataSource = yaopins;
            toolStripComboBoxDrug.ComboBox.DisplayMember = "YaopinLongName";
            toolStripComboBoxDrug.ComboBox.ValueMember = "YaopinID";
            toolStripComboBoxDrug.ComboBox.SelectedIndex = 0;
        }

        private void LoadAccountList()
        {
            if (accountTyep == AccountType.DayAccount)
            {
                List<YaopinAccount> ya = AnesthesiaBLL.GetDayAccount(dayPicker.Value);
                LoadAccountList(ya);
            }
            else if (accountTyep == AccountType.DrugAccount)
            {
                List<YaopinAccount> ya = AnesthesiaBLL.GetDrugAccount(toolStripComboBoxDrug.ComboBox.SelectedValue.ToString(), startPicker.Value, endPicker.Value);
                LoadAccountList(ya);
            }
        }

        private void LoadAccountList(List<YaopinAccount> ya)
        {
            listView1.Items.Clear();
            int i = 1;

            foreach (YaopinAccount y in ya)
            {
                ListViewItem lvit = new ListViewItem(i.ToString());
                lvit.ImageIndex = 0;
                lvit.Tag = y;
                i++;

                if (i % 2 == 1)
                    lvit.BackColor = Color.FromArgb(255, 192, 255, 255);
                else
                    lvit.BackColor = Color.FromArgb(255, 255, 255, 128);

                ListViewItem.ListViewSubItem lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.AccountDate.ToShortDateString();
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.Yaopin.YaopinName;
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.Yaopin.YaopinGuige;
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.Yaopin.YaopinDanwei;
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.SumPurchase.ToString();
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.SumUsageTotal.ToString();
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.SumCurrent.ToString();
                lvit.SubItems.Add(lvsit);

                listView1.Items.Add(lvit);
            }
        }

        private void toolStripComboBoxDrug_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!initCompleted)
                return;

            try
            {
                LoadAccountList();
            }
            catch (Exception ex)
            {
                MessageBox.Show("读取数据失败！", "医院", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
